<template>
	<view>
		<view class="imgvideo">
			<text :class="{video_title : isimg}" @click="totaggle(2)">借用物资</text>
			<text :class="{video_title : !isimg}" @click="totaggle(1)">领用物资</text>
		</view>
		<view class="u-flex u-row-center" style="background-color: #fff;">
			<text style="width:20%;text-align: center;">时间</text>
			<view style="width:80%;">
				<view class="select_title u-padding-20" @click="show = true">
					<input v-model="model1.time" disabled class="dis_inl_blo va_m u-margin-right-20" type="text" placeholder="请选择时间">>
					<!-- <u-button class="dis_inl_blo va_m" size="mini" type="success" @click="show = true">打开</u-button> -->
				</view>
			</view>
		</view>
		<view class="u-margin-top-10" style="padding-bottom:100rpx">
			<view class="u-menu-wrap" style="background-color: #fff;">
				<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
					<view v-for="(item,index) in tabbar" :key="index" class="u-tab-item"
						:class="[current==index ? 'u-tab-item-active' : '']" :data-current="index"
						@tap.stop="swichMenu(index)">
						<text class="u-line-1">{{item.name}}</text>
					</view>
				</scroll-view>
				<block v-for="(item,index) in tabbar" :key="index">
					<scroll-view scroll-y class="right-box" v-if="current==index">
						<view class="page-view">
							<view class="class-item">
								<view class="item-container">
									<view class="thumb-box u-flex u-row-between" v-for="(item1, index1) in item.foods" :key="index1">
										
										<view class="item-menu-name">
											<text>{{item1.name}}</text>
											<text style="font-size: 26rpx;color:#999;margin-left:50rpx;">+{{item1.stock}}</text>
										</view>
										<view v-if="item1.stock == 0" style="">
											<text>库存不足</text>
										</view>
										<view v-if="item1.status == 1" style="" >
											<u-icon name="plus-circle-fill" color="#008987" size="40" @click="plus(item1)"></u-icon>
											<u-icon :customStyle="customStyle" name="minus-circle" color="#008987" size="40" @click="minus(item1)"></u-icon>
										</view>
										<!-- <view>
											<u-number-box v-model="value" @change="valChange"></u-number-box>
										</view> -->
										
									</view>
								</view>
							</view>
						</view>
					</scroll-view>
					<!-- <u-empty v-if="tabbar[index].foods.length==0"  :show="true" mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png">
					</u-empty> -->
				</block>
				
			</view>
		</view>
		<view class="uni-cell-90 u-flex u-row-between" style="width: 100%;background-color: #fff; position: fixed; bottom: 0rpx;padding:30rpx 0;">
			<view style="width:25%" class="u-text-center" @click="toreceiveList">
				<u-icon name="file-text-fill" size="40" color="#008987"></u-icon>
				<text class="dis_blo">借领记录</text>
			</view>
			<view class="iconType2" style="width:75%;">
				<view class="text1">
					<u-icon name="https://s4.ax1x.com/2022/02/23/b9Wdzt.png" size="30" :label="'资产箱(' + num + ')'"></u-icon>
				</view>
				<view class="text2" @click="next">
					提交
				</view>
			</view>
		</view>
		<u-toast ref="uToast" :loading="true"></u-toast>
		<u-picker mode="time" v-model="show" :params="params" @confirm="confirm"></u-picker>
		
	</view>
</template>

<script>
	import {
		getAssetsData
	} from "@/service/assetService.js";
	export default {
		data () {
			return {
				isimg:true,
				show: false,
				params: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: true
				},
				// time:'',
				// startDate:'',
				// endDate:'',
				customStyle:{
					marginLeft:'20rpx'
				},
				model1: {
					type:2,
					time: '',
					nid: [],
					tools: [],
				},
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				num:0,
				tabbar: [],
			}
		},
		mounted() {
			getAssetsData().then(this.getAssetsData);
		},
		methods:{
			getAssetsData(e){
				if(e.code==200){
					this.tabbar=e.result
				}
			},
			toreceiveList(){
				uni.navigateTo({
					url:'./receiveList'
				})
			},
			totaggle:function(num){
				this.model1.type=num;
				if(num==2){
					this.isimg=true
				}else{
					this.isimg=false
				}
			},
			plus(item1) {
				var a = this.model1.nid.indexOf(item1.cat);
				if(a > -1){
					
					for(var i=0;i<this.model1.tools.length;i++){
						if(item1.stock>this.model1.tools[i].num){
							if(item1.cat==this.model1.tools[i].nid){
								this.num++;
								this.model1.tools[i].num++;
							}
						}else{
							this.$refs.uToast.show({
								type: 'error',
								title: '库存不够',
								icon:false
							})
						}
					}
				}else{
					this.num++;
					this.model1.nid.push(item1.cat);
					this.model1.tools.push({name:item1.name,nid:item1.cat,num:1});
				}
				// console.log(this.model1.tools);
			},
			minus(item1) {
				
				var a = this.model1.nid.indexOf(item1.cat);
				if(a > -1){
					
					for(var i=0;i<this.model1.tools.length;i++){
						if(this.model1.tools[i].num>0){
							if(item1.cat==this.model1.tools[i].nid){
								this.num--;
								this.model1.tools[i].num--;
							}
						}else{
							this.$refs.uToast.show({
								type: 'error',
								title: '还未添加到物资库',
								icon:false
							})
						}
					}
				}else{
					this.$refs.uToast.show({
						type: 'error',
						title: '还未添加到物资库',
						icon:false
					})
				}
			},
			next() {
				// console.log(this.model1);
				if(this.model1.time == ''){
					this.$refs.uToast.show({
					
						type: 'error',
						title: '请选择预约时间',
						icon:false
					})
					return;
				}
				uni.navigateTo({
					url:'./receiveAssetDetail?item='+encodeURIComponent(JSON.stringify(this.model1))
				})
			},
			
			confirm(e){
				// console.log(e)
				this.model1.time=e.year+'-'+e.month+'-'+e.day+' '+e.hour+':'+e.minute+':'+e.second
			},
			
			// 点击左边的栏目切换
			async swichMenu(index) {
				if (index == this.current) return;
				this.current = index;
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			},
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #f8f8f8;
	}
	.imgvideo{
		display: block;
		width:95%;
		margin:30rpx auto;
		border:1rpx solid #008987;
		border-radius: 10rpx;
		overflow: hidden;
		text{
			display: inline-block;
			height:60rpx;
			width:50%;
			color:#008987;
			text-align: center;
			line-height: 60rpx;
		}
		.video_title{
			color:#fff;
			background-color: #008987;
		}
	}
	.select_title input{
		width:85%;
		height:50rpx;
		padding:0 10rpx;
		font-size: 26rpx;
	}
	.top_title{
		color: $u-type-primary;
		text-align: center;
		padding: 20rpx 0 0 0;
	}
	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}
	
	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}
	
	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}
	
	.u-tab-view {
		width: 200rpx;
		height: 100%;
		border-top-right-radius:20rpx;
		background-color: #f8f8f8;
		overflow: hidden;
	}
	
	.u-tab-item {
		height: 110rpx;
		// background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}
	
	.u-tab-item-active {
		position: relative;
		color: #008987;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
	}
	
	.u-tab-item-active::before {
		content: "";
		position: absolute;
		// border-left: 4px solid red;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}
	
	.u-tab-view {
		height: 100%;
	}
	
	.right-box {
	}
	
	.page-view {
		padding: 16rpx;
	}
	
	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}
	
	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}
	
	.item-menu-name {
		font-weight: normal;
		font-size: 28rpx;
		color: $u-main-color;
	}
	
	.item-container {
		// display: flex;
		// flex-wrap: wrap;
	}
	
	.thumb-box {
		width: 100%;
		padding: 20rpx 0;
	}
	
	.item-menu-image {
		width: 120rpx;
		height: 120rpx;
	}
	.iconType2 {
		
		display: flex;
		border: #E1E1E1 1rpx solid;
		border-radius: 40rpx;
		justify-content: space-between;
		// margin-left: 5%;
		height: 80rpx;
		line-height: 80rpx;
		background-color: #FFFFFF;
	
		.text1 {
			color: red;
			margin-top: 10rpx;
			width: 70%;
			text-align: center;
			// margin-left: 200rpx;
		}
	
		.text2 {
			color: #FFFFFF;
			text-align: center;
			width: 30%;
			background-image: url("https://resourse.cnlhjt.com/upload/20220825/79b8644697f64c4b587f78e6d891f1e8.png");
			background-size: 100% 100%;
		}
	}
</style>